package com.kedll.library.widget;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.kedll.library.util.Log;
import com.kedll.library.util.MyUtils;


/**
 * TODO: document your custom view class.
 */
public class SeekBar extends View {

    private int r = 25;
    private TextPaint mTextPaint;
    private Paint paint = new Paint();
    private Paint circlePaint = new Paint();//外圆
    private float mTextWidth;
    private float mTextWidthA;
    private float mTextWidthB;
    private float mTextHeight;

    private int currentSelectedIndex = 1;//当前选中的
    private OnTextSizeSelectListener textSizeSelectListener;

    private boolean isDrag = false;

    private int NUM = 4;

    public void setOnTextSizeSelectListener(OnTextSizeSelectListener textSizeSelectListener) {
        this.textSizeSelectListener = textSizeSelectListener;
    }

    int paddingLeft;
    int paddingRight;
    int paddingTop;
    int paddingBottom;

    int contentWidth;
    int contentHeight;

    public SeekBar(Context context) {
        super(context);
        init(null, 0);
    }

    public SeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public SeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {


        // Set up a default TextPaint object
        mTextPaint = new TextPaint();
        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.LEFT);

        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(2.0f);

        circlePaint.setAntiAlias(true);
        circlePaint.setMaskFilter(new BlurMaskFilter(r, BlurMaskFilter.Blur.SOLID));
        // Update TextPaint and text measurements from attributes
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        int width = getWidth();
        int height = getHeight();
        if (width > 0 && height > 0) {
            paddingLeft = getPaddingLeft();
            paddingRight = getPaddingRight();
            paddingTop = getPaddingTop();
            paddingBottom = getPaddingBottom();

            if (paddingLeft == 0) {
                paddingLeft = MyUtils.dp2px(40);
            }
            if (paddingRight == 0) {
                paddingRight = MyUtils.dp2px(40);
            }
            if (paddingTop == 0) {
                paddingTop = MyUtils.dp2px(40);
            }
            if (paddingBottom == 0) {
                paddingBottom = MyUtils.dp2px(40);
            }

            contentWidth = width - paddingLeft - paddingRight;
            contentHeight = height - paddingTop - paddingBottom;

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // allocations per draw cycle.


        mTextPaint.setTextSize(MyUtils.dp2px(14));
        mTextPaint.setColor(Color.BLACK);
        mTextWidthA = mTextPaint.measureText("A");

        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mTextHeight = fontMetrics.bottom;

        // Draw the text.
        canvas.drawText("A",
                paddingLeft - (mTextWidthA / 2),
                paddingTop + (contentHeight + mTextHeight) / 3,
                mTextPaint);


        mTextPaint.setTextSize(MyUtils.dp2px(16));
        mTextPaint.setColor(Color.GRAY);
        mTextWidth = mTextPaint.measureText("标准");

        canvas.drawText("标准",
                paddingLeft + contentWidth / NUM - mTextWidth / 2,
                paddingTop + (contentHeight + mTextHeight) / 3,
                mTextPaint);

        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(MyUtils.dp2px(20));
        mTextWidthB = mTextPaint.measureText("A");

        canvas.drawText("A",
                contentWidth + paddingLeft - (mTextWidthB / 2),
                paddingTop + (contentHeight + mTextHeight) / 3,
                mTextPaint);


        float lineY = paddingTop + (contentHeight + mTextHeight) * 3 / 4;
        canvas.drawLine(paddingLeft, lineY, contentWidth + paddingRight, lineY, paint);

        for (int i = 0; i < 5; i++) {
            int left = paddingLeft + i * (contentWidth / NUM);
            int right = left;
            canvas.drawLine(left, lineY - 8, right, lineY + 8, paint);
        }


        canvas.save();
        canvas.translate(paddingLeft + currentSelectedIndex * (contentWidth / NUM), lineY);

        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setColor(Color.WHITE);
        canvas.drawCircle(0, 0, r, circlePaint);

        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setColor(Color.GRAY);
        canvas.drawCircle(0, 0, r, circlePaint);

        canvas.restore();

    }


    public void setCurrentSelectedIndex(int currentSelectedIndex) {
        this.currentSelectedIndex = currentSelectedIndex;
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {


        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                float downX = event.getX() - paddingLeft;
                int lineY = (int) (paddingTop + (contentHeight + mTextHeight) * 3 / 4);
                float downY = event.getY();
                Rect rect = new Rect(0, lineY - r, getWidth(), lineY + 2 * r);
                if (rect.contains((int) downX, (int) downY)) {
                    int selectedIndex = Math.max(0, Math.round(downX / (contentWidth / NUM)));
                    if (currentSelectedIndex != selectedIndex) {//当前点击的位置还未选择,则跳转到当前位置
                        currentSelectedIndex = selectedIndex;
                        Log.i("SeekBar", selectedIndex);
                        invalidate();
                        if (textSizeSelectListener != null) {
                            textSizeSelectListener.onTextSizeSelection(currentSelectedIndex);
                        }

                    } else {
                        isDrag = true;
                        return true;
                    }
                }
            }
            break;
            case MotionEvent.ACTION_MOVE:
                if (isDrag) {
                    float downX = event.getX() - paddingLeft;
                    int selectedIndex = Math.max(0, Math.round(downX / (contentWidth / NUM)));
                    if (currentSelectedIndex != selectedIndex) {//当前点击的位置还未选择,则跳转到当前位置
                        currentSelectedIndex = selectedIndex;
                        invalidate();
                        if (textSizeSelectListener != null) {
                            textSizeSelectListener.onTextSizeSelection(currentSelectedIndex);
                        }
                    }
                }

                break;
            case MotionEvent.ACTION_UP:
                isDrag = false;
                break;
        }

        return super.onTouchEvent(event);
    }


    public interface OnTextSizeSelectListener {
        void onTextSizeSelection(int index);
    }
}



